import { loginAPI, getUserInfoAPI, getEmployeeBaseInfoAPI } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userinfo: {} // 存储用户信息
  },
  mutations: {
    // 设置token
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    // 删除token
    removeToken(state) {
      state.token = undefined
      removeToken()
    },
    // 设置用户个人信息
    setUserInfo(state, data) {
      state.userinfo = data
    },
    // 删除用户个人信息
    removeUserInfo(state) {
      state.userinfo = {}
    }
  },
  actions: {
    // 登录1.存储token
    async loginFn(context, data) {
      try {
        const res = await loginAPI(data)
        context.commit('setToken', res)
      } catch (error) {
        console.log(error, 'user')
      }
    },
    // 退出登录 1. 清除用户数据
    logout(context) {
      // 1.1 清除用户的token
      context.commit('removeToken')
      // 2.2 清除用户个人信息
      context.commit('removeUserInfo')
    },
    // 获取个人用户信息API
    async getUserInfos(context) {
      const a = await getUserInfoAPI()
      const b = await getEmployeeBaseInfoAPI(a.userId)
      // b.staffPhoto = null
      // console.log(b, 'userinfo')
      context.commit('setUserInfo', { ...a, ...b })
    }
  }
}
